package com.power.home.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.power.home.common.Constant;
import com.power.home.common.util.DrawableUtil;
import com.power.home.common.util.PopuUtil;
import com.power.home.common.util.UIUtil;
import com.power.home.data.bean.ConvertCodeListItemBean;
import com.power.home.data.bean.ConvertRecordBean;
import com.power.home.data.bean.CreateCodeBean;
import com.power.home.data.bean.InviteRecordBean;
import com.power.home.di.component.AppComponent;
import com.power.home.di.component.DaggerConvertRecordComponent;
import com.power.home.di.module.ConvertRecordModule;
import com.power.home.presenter.ConvertRecordPresenter;
import com.power.home.presenter.contract.ConvertRecordContract;
import com.power.home.ui.adapter.InviteRecordAdapter;
import com.power.home.ui.widget.MyPopupWindow;
import com.power.home.ui.widget.MyTitleBar;
import com.power.home.R;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = "/android/codeConvertList")
public class CodeConvertListActivity extends BaseActivity<ConvertRecordPresenter> implements ConvertRecordContract.View{

    @BindView(R.id.titleBar)
    MyTitleBar titleBar;
    @BindView(R.id.recycle_convert)
    RecyclerView recycleConvert;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private boolean useStatus;
    private int page = 0;
    private int size = 20;
    UserInfoAdapter userInfoAdapter;
    CodeInfoAdapter codeInfoAdapter;
    BaseQuickAdapter currentAdapter;
    @Override
    public void getCodeMarketRecordSuccess(List<ConvertRecordBean> convertRecordBeans) {

    }

    @Override
    public void codeMarketGenerateSuccess(CreateCodeBean createCodeBean) {
        MyPopupWindow mLoginOutPopupWindow = new PopuUtil().initAtLocationPopuWrap(this,
                R.layout.popup_convertcode);
        TextView tv_code = mLoginOutPopupWindow.getContentView().findViewById(R.id.tv_code);
        tv_code.setText(createCodeBean.getExchangeCode());
        Button buttonPopupCancel = mLoginOutPopupWindow.getContentView().findViewById(R.id.button_creat_cancel);
        Button buttonPopupCommitSure = mLoginOutPopupWindow.getContentView().findViewById(R.id.button_creat_commit_sure);
        buttonPopupCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLoginOutPopupWindow.dismiss();
                refreshLayout.autoRefresh();

            }
        });
        buttonPopupCommitSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLoginOutPopupWindow.dismiss();
                copyText(createCodeBean.getExchangeCode());
                refreshLayout.autoRefresh();

            }
        });
    }

    @Override
    public void codeMarketListSuccess(List<ConvertCodeListItemBean> convertCodeListItemBeans) {
        if (refreshLayout != null) {
            refreshLayout.finishLoadMore();
            refreshLayout.finishRefresh();
        }
        if (convertCodeListItemBeans.size() == 0) {
            if (page == 0) {
                currentAdapter.setNewData(convertCodeListItemBeans);

                currentAdapter.setEmptyView(getEmptyView(R.drawable.icon_empty_team, "暂无信息，快去邀请吧"));
            }
        } else {
            if (page == 0) {
                currentAdapter.setNewData(convertCodeListItemBeans);
            } else {
                currentAdapter.addData(convertCodeListItemBeans);
            }
            page++;
        }
        if (convertCodeListItemBeans.size() < size) {
            refreshLayout.finishLoadMoreWithNoMoreData();

        }
    }

    @Override
    public int setLayout() {
        return R.layout.activity_codeconvertlist;
    }

    @Override
    public void getExtras() {
        useStatus = getIntent().getBooleanExtra(Constant.id,false);
        mPresenter.codeMarketList(useStatus,page,size);
    }

    @Override
    public boolean init() {



        titleBar.setTitle(useStatus ? "未用名额" : "已用名额");

        if(useStatus){
            titleBar.setRightLayoutVisibility(View.VISIBLE);
            titleBar.setRightLayoutClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPresenter.createCodeMarket();
                }
            });
        }else{
            titleBar.setRightLayoutVisibility(View.GONE);
        }
        initView();
        return false;
    }
    private void copyText(String text){
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText(null,text);
        clipboard.setPrimaryClip(clipData);
        UIUtil.showToastSafe("复制成功");
    }
    @Override
    public void setListener() {
        backListener(titleBar);
    }

    @Override
    public void showError(String msg) {
        if(!TextUtils.isEmpty(msg)){
            UIUtil.showToastSafe(msg);
        }

        if (refreshLayout != null) {
            refreshLayout.finishLoadMore();
            refreshLayout.finishRefresh();
        }
    }

    private void createAdapter(){
        if(!useStatus){
            userInfoAdapter = new UserInfoAdapter();
            currentAdapter = userInfoAdapter;
            userInfoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {


                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    ConvertCodeListItemBean itemBean = (ConvertCodeListItemBean)adapter.getItem(position);
                    ARouter.getInstance().build("/android/vipDetailInfo")
                            .withInt(Constant.id, itemBean.getRecordDto().getUserId())
                            .navigation();
                }
            });

            userInfoAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    ConvertCodeListItemBean team = (ConvertCodeListItemBean) adapter.getItem(position);
                    String phone = team.getRecordDto().getPhone();
                    switch (view.getId()) {
                        case R.id.iv_item_team_phone:
                            UIUtil.goCall(phone, CodeConvertListActivity.this);
                            break;
                        case R.id.iv_item_team_info:
                            UIUtil.goSMS(phone, CodeConvertListActivity.this);
                            break;
                    }
                }

            });


        }else{
            codeInfoAdapter = new CodeInfoAdapter();
            currentAdapter = codeInfoAdapter;
            codeInfoAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    ConvertCodeListItemBean team = (ConvertCodeListItemBean) adapter.getItem(position);
                    copyText(team.getExchangeCode());
                }
            });
        }
    }
    private void initView(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(UIUtil.getContext(), LinearLayoutManager.VERTICAL, false);
        recycleConvert.setLayoutManager(linearLayoutManager);

        createAdapter();
        recycleConvert.setAdapter(currentAdapter);

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.codeMarketList(useStatus,page, size);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 0;
                mPresenter.codeMarketList(useStatus,page, size);
            }
        });
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

        DaggerConvertRecordComponent.builder().appComponent(appComponent)
                .convertRecordModule(new ConvertRecordModule(this))
                .build().inject(this);
    }
    public class UserInfoAdapter extends BaseQuickAdapter<ConvertCodeListItemBean, BaseViewHolder> {

        public  UserInfoAdapter(){
            super(R.layout.item_team,null);
        }
        @Override
        protected void convert(BaseViewHolder helper, ConvertCodeListItemBean item) {
            DrawableUtil.loadUrl(R.drawable.icon_avatar_header, helper.getView(R.id.civ_item_team), item.getRecordDto().getAvatar());
            helper.setText(R.id.iv_item_team_name, item.getRecordDto().getNickName());
            TextView tv_title = helper.getView(R.id.tv_item_expire_title);
            TextView tv_expire = helper.getView(R.id.tv_item_exprie_time);
            if(TextUtils.isEmpty(item.getRecordDto().getHappinessVipDueTime())){
                tv_title.setVisibility(View.GONE);
                tv_expire.setVisibility(View.GONE);

            }else{
                tv_title.setVisibility(View.VISIBLE);
                tv_expire.setVisibility(View.VISIBLE);
                helper.setText(R.id.tv_item_exprie_time, item.getRecordDto().getHappinessVipDueTime());
            }

            helper.addOnClickListener(R.id.iv_item_team_phone);
            helper.addOnClickListener(R.id.iv_item_team_info);



        }
    }

    public class CodeInfoAdapter extends BaseQuickAdapter<ConvertCodeListItemBean, BaseViewHolder> {

        public  CodeInfoAdapter(){
            super(R.layout.team_invite_code_item,null);
        }
        @Override
        protected void convert(BaseViewHolder helper, ConvertCodeListItemBean item) {
          TextView tv_code = helper.getView(R.id.tv_codenumber);
          tv_code.setText(item.getExchangeCode());
            LinearLayout line_codecopy = helper.getView(R.id.line_codecopy);
            helper.addOnClickListener(R.id.line_codecopy);

        }
    }
}
